package main

import (
	"fmt"
	"os"

	genindex "markdown_gen_index/src/gen_index"

	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use:   "mdgenidx",
	Short: "mdgenidx is a command line tool used to recursively generate markdown indexes in directories.",
	Long:  `mdgenidx is a command line tool used to recursively generate markdown indexes in directories.`,
	Run:   func(cmd *cobra.Command, args []string) {},
}

func Execute() {
	err := rootCmd.Execute()
	if err != nil {
		fmt.Println("failed in rootcmd Execute()")
		os.Exit(1)
	}
}

var genCmd = &cobra.Command{
	Use:   "gen",
	Short: "Generate markdown index",
	Long:  `Generate markdown index`,
	Run: func(cmd *cobra.Command, args []string) {
		run()
	},
}

var indexOpt = &genindex.IndexOption{}
var genOpt = &genindex.GenerationOption{}

func run() {
	genindex.NewIndex(indexOpt).Generate(genOpt)
}

var appVersion = "v0.0.1"

var versionCmd = &cobra.Command{
	Use:   "version",
	Short: "Version",
	Long:  "Version",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Printf("version: %s\n", appVersion)
	},
}

func init() {
	// version args
	rootCmd.AddCommand(versionCmd)
	// gen args
	genCmd.Flags().StringVarP(&indexOpt.WorkDir, "workdir", "d", ".", "Specify the directory to generate markdown index.")
	genCmd.Flags().StringVarP(&indexOpt.IndexTitle, "index-title", "t", "", "Specify the title of markdown index, default is title of markdown index file or current directory name.")
	genCmd.Flags().StringVar(&indexOpt.HomeTitle, "home-title", "", "Specify the title of home link in markdown index, if not specified, use `index-title`.")
	genCmd.Flags().StringVarP(&indexOpt.RootIndexFile, "root-index-file", "f", ".autogen.md", "Specify the markdown root index file, default is `.autogen.md`.")
	genCmd.Flags().StringVar(&indexOpt.SubIndexFile, "sub-index-file", ".autogen.md", "Specify the markdown sub index file, default is `.autogen.md`.")
	genCmd.Flags().BoolVar(&indexOpt.InheritGitIgnore, "inherit-gitignore", true, "Use `.gitignore` file as ignore file, default is `true`.")
	genCmd.Flags().BoolVar(&genOpt.Override, "override", false, "Override markdown existing index file, default is `false`.")
	genCmd.Flags().BoolVarP(&genOpt.Recursive, "recursive", "r", false, "Recursively generate markdown index in subdirectories, default is `false`.")
	genCmd.Flags().BoolVar(&genOpt.NoHeaderLink, "no-header-link", false, "Do not generate header link in index file, default is `false`.")
	genCmd.Flags().BoolVar(&genOpt.Nav, "nav", false, "Generate navigation in markdown file, default is `false`.")
	genCmd.Flags().BoolVarP(&genOpt.Verbose, "verbose", "v", false, "Show verbose log, default is `false`.")

	rootCmd.AddCommand(genCmd)
}

func main() {
	Execute()
}
